package com.hornsun.repository;

import com.hornsun.domain.dbo.ContentType;
import com.hornsun.domain.dbo.ContentTypeExample;
import com.hornsun.repository.mapper.ContentTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by 编译中... on 2018/4/14.
 */
@Repository
public class ContentTypeRepository{

    @Resource
    private ContentTypeMapper contentTypeMapper;

    public List<ContentType> queryContentType(int type){
        ContentTypeExample contentTypeExample = new ContentTypeExample();
        ContentTypeExample.Criteria criteria = contentTypeExample.createCriteria();
        criteria.andTypeEqualTo((byte)type);
        contentTypeExample.setOrderByClause("type_index");
        return contentTypeMapper.selectByExample(contentTypeExample);
    }

    public List<ContentType> queryFirstType(int type ){
        ContentTypeExample contentTypeExample = new ContentTypeExample();
        contentTypeExample.setOrderByClause("type_index");
        ContentTypeExample.Criteria criteria = contentTypeExample.createCriteria();
        criteria.andTypeEqualTo((byte)type);
        return contentTypeMapper.selectByExample(contentTypeExample);
    }

    //根据type删除
    public void deleteByType(int type){
        ContentTypeExample contentTypeExample = new ContentTypeExample();
        ContentTypeExample.Criteria criteria = contentTypeExample.createCriteria();
        criteria.andTypeEqualTo((byte)type);
        contentTypeMapper.deleteByExample(contentTypeExample);
    }

    //插入
    public void insert(ContentType contentType){
        if (contentTypeMapper.insertSelective(contentType)!=1){
            throw new IllegalArgumentException("insert contentType failed by contentType:"+contentType);
        }
    }

    //通过guid修改name
    public void update(ContentType contentType){
        int count = contentTypeMapper.updateByPrimaryKeySelective(contentType);
        if (count!=1){
            throw new IllegalArgumentException("update contentType failed by contentType:"+contentType);
        }
    }




}
